package com.fan.tank;

import com.fan.game.GameFrame;
import com.fan.util.Constant;
import com.fan.util.EnemyTanksPool;
import com.fan.util.MyUtil;

import javax.naming.Name;
import java.awt.*;

/**
 * 敌人坦克类
 */
public class EnemyTank extends Tank {
    //记录5秒开始的时间时间
    private long aiTime;
    private static Image[] enemyImg;

    static {
        enemyImg = new Image[4];
        enemyImg[0] = MyUtil.createImage("res/ul.png");
        enemyImg[1] = MyUtil.createImage("res/dl.png");
        enemyImg[2] = MyUtil.createImage("res/ll.png");
        enemyImg[3] = MyUtil.createImage("res/rl.png");
    }

    public EnemyTank() {
        aiTime = System.currentTimeMillis();
    }

    private EnemyTank(int x, int y, int dir) {
        super(x, y, dir);
        //敌人一旦出现就计时
        aiTime = System.currentTimeMillis();

        }

    public static Tank createEnemy(){
        int x = MyUtil.getRandomNumber(0,2) == 0 ? RADIUS :
                Constant.FRAME_WIDTH-RADIUS;
        int y = GameFrame.titleBarh+RADIUS;
        int dir = DIR_DOWN;
        Tank enemy = EnemyTanksPool.get();
        enemy.setX(x);
        enemy.setY(y);
        enemy.setDir(dir);
        enemy.setEnemy(true);
        // TODO: 2021/10/6
        enemy.setState(STATE_MOVE);
        enemy.setHp(Tank.DEFAULT_HP);
        return enemy;
    }

        public void drawImgTank(Graphics g){
            ai();
            g.drawImage(enemyImg[getDir()], getX() - RADIUS, getY() - RADIUS, null);

        }

    /**
     * 敌人的AI
     */
    private void ai(){
            if(System.currentTimeMillis() - aiTime > Constant.ENEMY_AI_INTERVAL){
                //间隔五秒切换一个随机状态
                setState(MyUtil.getRandomNumber(0,2) == 0 ? STATE_STAND : STATE_MOVE);
                setDir(MyUtil.getRandomNumber(DIR_UP,DIR_RIGHT+1));
                aiTime = System.currentTimeMillis();
            }
            //比较小的概率去开火
            if(Math.random() < Constant.ENEMY_FIRE_PERCENT){
                fire();
            }

        }


    }
